Create Operation

For the Create operation, you will implement a route that allows users to add new transactions. This will involve handling both GET and POST HTTP requests - GET for displaying the form to the user and POST for processing the form data sent by the user.

Here is the list of steps to implement the Create operation.

  1. Create a function named add_transaction.

  2. Use add as the decorator for this function. Make sure to pass both GET and POST as possible methods.

  3. If the request method is GET, use the render_template function to display an HTML form using a template named form.html. This form will allow users to input data for a new transaction.

  4. If the request method is POST, use request.form to extract the form data, create a new transaction, append it to the transactions list, and then use redirect and url_for to send the user back to the list of transactions.

  5. The new transaction is passed on to the reading function in the following format.

  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  1. transation = {
  2. 'id': len(transactions)+1
  3. 'date': request.form['date']
  4. 'amount': float(request.form['amount'])
  5. }

Here, request.form function parses the information received from the entry made in the form.

Click here for hint The add_transaction function content needs the following implementations.

For POST method, create the new transaction as shown above, append it to the existing list of transactions and redirect to the URL for Read operation.

For GET method, render the form.html page that accepts the information from the interface.

Click here for solution
  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
  7. 7
  8. 8
  9. 9
  10. 10
  11. 11
  12. 12
  13. 13
  14. 14
  15. 15
  16. 16
  17. 17
  18. 18
  19. 19
  20. 20
  1. # Create operation: Display add transaction form
  2. # Route to handle the creation of a new transaction
  3. @app.route("/add", methods=["GET", "POST"])
  4. def add_transaction():
  5. # Check if the request method is POST (form submission)
  6. if request.method == 'POST':
  7. # Create a new transaction object using form field values
  8. transaction = {
  9. 'id': len(transactions) + 1, # Generate a new ID based on the current length of the transactions list
  10. 'date': request.form['date'], # Get the 'date' field value from the form
  11. 'amount': float(request.form['amount']) # Get the 'amount' field value from the form and convert it to a float
  12. }
  13. # Append the new transaction to the transactions list
  14. transactions.append(transaction)
  15. # Redirect to the transactions list page after adding the new transaction
  16. return redirect(url_for("get_transactions"))
  17. # If the request method is GET, render the form template to display the add transaction form
  18. return render_template("form.html")

Now, the code will look like this:
Correct code

Note: The statements outside the if case are, by default, the else case. The statements in the if case end with a return statement; hence only one of the two cases will run at a time.